﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;

namespace DAO
{
    public class HangHoaDAO
    {
        public static List<HangHoa> loadAll()
        {
            List<HangHoa> ds = new List<HangHoa>();

            using (var db = new bcsaleEntities())
            {
                ds = db.HangHoas.ToList<HangHoa>();
            }

            return ds;                        
        }

        public static List<HangHoaDTO> loadDSHangHoa()
        {
            try
            {
                using (var db = new bcsaleEntities())
                {
                    var query = from hh in db.HangHoas
                                from dvt in db.DonViTinhs
                                from nh in db.NhomHangs
                                from xx in db.XuatXus
                                where hh.MaDVT == dvt.ID
                                where hh.MaNhomHang == nh.ID
                                where hh.MaXuatXu == xx.ID
                                select new { hh, dvt, nh, xx };

                    var result = query.AsEnumerable().Select((kq, idx) => new HangHoaDTO() { GhiChu = kq.hh.GhiChu, GiaBanLe = Convert.ToDouble(kq.hh.GiaBanLe)
                                                                                            , GiaBanSi = Convert.ToDouble(kq.hh.GiaBanLe), GiaMua = Convert.ToDouble(kq.hh.GiaMua)
                                                                                            , MaHang = kq.hh.MaHang, NhomHang = kq.nh.TenLoai, TenDVT = kq.dvt.TenDVT, TenHang = kq.hh.TenHang, VAT = Convert.ToDouble(kq.hh.VAT)
                                                                                            , XuatXu = kq.xx.TenXuatXu, IDDVT = kq.dvt.ID , IDHangHoa = kq.hh.ID, IDNhomHang = kq.nh.ID, IDXuatXu = kq.xx.ID});
                    return result.ToList<HangHoaDTO>();
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
    }
}
